package top.superflow.utils

import java.net.URL
import java.util.jar.JarFile

import top.superflow.lang.StringUtils
import top.superflow.log.Loggable

import scala.collection.mutable

object JarUtils extends Loggable{



  def getAllJarFiles: Set[JarFile] = {
    val resources = new mutable.HashSet[JarFile]
    val jarPaths = System.getProperty("java.class.path")
    for (jarPath <- StringUtils.split(jarPaths, StringUtils.SEMICOLON)) {
      addJar(resources, jarPath)
    }

    getClass.getProtectionDomain.getCodeSource.getLocation.getFile

    resources.toSet
  }

  private def createJarFile(jarPath : String): JarFile = {
    try {
      return new JarFile(jarPath)
    } catch {
      case exception: Exception => {
        logger.warn("{} doesn't exist", jarPath)
        return null
      }
    }
  }

  private def addJar(resources : mutable.Set[JarFile], jarPath: String): Unit ={
    val jarFile = createJarFile(jarPath)

    if(jarFile == null){
      return
    }

    resources.add(jarFile)
  }

  def main(args: Array[String]): Unit = {
    System.out.print(System.getProperty("java.class.path"))
  }
}
